<template>
  <div>
    <div class="accuser_input">
      <input type="text" v-model="search" placeholder="请输入搜索关键字" >
      <span @click="searchall">搜索</span>
    </div>
    <div class="search_content">

      <div class="beforesearch" v-if="haveeno == 0">
        <div class="search_tip">
          <p >搜索历史 <span>自动保留最近10条搜索历史</span></p>
          <p @click="clean">清除</p>
        </div>
        <HistoryList :historylist="historylist" :historylistlength="historylistlength" v-on:getinput="showgetinput" v-on:listerson="showdelete"></HistoryList>
      </div>

      <div class="unsearch" v-if="haveeno == 1">
        <p>在" <strong>{{search}}</strong> "下未找到任何内容</p>
        <p>请更换关键词进行搜索</p>
      </div>

      <div v-if="haveeno == 2">
        <SearchedList :searchlist="zhuanglan" v-if="zhuanglan.length !=0"></SearchedList>
        <SearchedList :searchlist="shipin"  v-if="shipin.length !=0">
          <template #searched_title>视频</template>
        </SearchedList>
        <SearchedList :searchlist="tuwen"  v-if="tuwen.length !=0">
          <template #searched_title>图文</template>
        </SearchedList>
      </div>

    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import Vue from 'vue'
import SearchedList from './components/SearchedList'
import HistoryList from './components/HistoryList'
import * as fetch from '@/api/fetch'
Vue.use(Dialog)

export default {
  name: 'Search',
  data () {
    return {
      haveeno: 0,
      search: '',
      zhuanglan: [],
      shipin: [],
      tuwen: [],
      historylist: [],
      historylistlength: ''
    }
  },
  components: {
    SearchedList,
    HistoryList
  },

  watch: {
    'search': function (e) {
      if (e == '') {
        this.haveeno = 0
        this.getSearchHistory()
      }
    }
  },

  methods: {
    showgetinput (data) {
      this.search = data
      this.getVideoListByKeyword()
    },

    showdelete(data){
      this.deleteSearch(data)
    },

    async deleteSearch (hid) {
      const res = await fetch.deleteSearch({
        id: hid
      })
      console.log(res)
      this.getSearchHistory()
    },

    searchall () {
      this.getVideoListByKeyword()
    },

    async deleteallSearch () {
      const res = await fetch.deleteSearch({
        type: 'all'
      })
      console.log(res)
    },

    clean () {
      Dialog.confirm({
        title: '提示',
        message: '确定清除所以历史记录吗'
      }).then(() => {
        this.deleteallSearch()
        this.getSearchHistory()
        console.log("shabi")
      }).catch(() => {
        // on cancel
      })
    },
    async getSearchHistory () {
      const res = await fetch.getSearchHistory({

      })
      console.log(res)
      if (res.errcode == 0) {
        this.historylist = res.data,
        this.historylistlength = res.data.length
      }
    },

    async getVideoListByKeyword () {
      const res = await fetch.getVideoListByKeyword({
        page: 1,
        keyword: this.search
      })
      console.log(res)
      if (res.errcode == 0) {
        if (res.data.column.data.length == 0 && res.data.image.data.length == 0 && res.data.video.data.length == 0) {
          this.haveeno = 1
        } else {
          console.log('mmm')
          this.haveeno = 2
          this.zhuanglan = res.data.column.data
          this.shipin = res.data.video.data
          this.tuwen = res.data.image.data
        }
      }
    }
  },
  mounted () {
    this.getSearchHistory()
  },
  activated() {
    this.search=""
  }
}
</script>

<style lang="less" scoped>
  .accuser_input{
    width: 100%;
    height: 70px;
    border-bottom: 5px solid #f4f4f4;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  input{
    width: 75%;
    margin-right: 10px;
    border:none;
    border-radius: 8px;
    height: 30px;
    line-height: 30px;
    color: #acacac;
    background: #f4f4f4;
    padding-left: 20px;
    font-size: 15px;
  }
  span{
    font-weight: 600;
    font-size: 16px;
  }
  }
  .search_content{
    .beforesearch {
      margin-left: 15px;
      margin-right: 15px;
      padding: 15px 0;
      font-size: 13px;
      color: #acacac;
      .search_tip{
        display: flex;
        width: 100%;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        p:first-child {
          font-size: 16px;
          color: black;
          span {
            font-size: 13px;
            color: #acacac;
            margin-left: 12px;
          }
        }
      }
    }
    .unsearch{
      margin-top: 10px;
      width: 100%;
      text-align: center;
      p{
        font-size: 13px;
        color: #acacac;
        margin-bottom: 10px;
        strong{
          color: #09bb07;
        }
      }
    }
  }
</style>
